﻿using System;
using System.Collections.Generic;

namespace SimpleFS.Core.Implementations
{
    public abstract class AbstractDirectory : IDirectory
    {
        protected IDirectory internalObject;

        public AbstractDirectory(IDirectory internalObject)
        {
            this.internalObject = internalObject;
        }

        public virtual IDirectory GetDirectory(string directoryName)
        {
            return this.internalObject.GetDirectory(directoryName);
        }

        public virtual IFile GetFile(string fileName)
        {
            return this.internalObject.GetFile(fileName);
        }

        public virtual string Name
        {
            get { return this.internalObject.Name; }
        }

        public virtual DateTime LastModifiedTime
        {
            get { return this.internalObject.LastModifiedTime; }
        }

        public virtual Dictionary<string, string> Attributes
        {
            get { return this.internalObject.Attributes; }
        }

        public virtual IDirectory ParentDirectory
        {
            get { return this.internalObject.ParentDirectory; }
        }

        public virtual bool Exists()
        {
            return this.internalObject.Exists();
        }

        public virtual void Close()
        {
            this.internalObject.Close();
        }

        public abstract IDirectory[] Directories
        {
            get;
        }

        public abstract IFile[] Files
        {
            get;
        }

        public virtual void Create()
        {
            this.internalObject.Create();
        }

        public abstract IFile AddFile(string fileName);

        public abstract IDirectory AddDirectory(string directoryName);

        #region IFileSystemObject Members

        public virtual void Copy(IFileSystemObject target)
        {
            IDirectory targetDirectory = target as IDirectory;

            if (!targetDirectory.Exists())
            {
                targetDirectory.Create();
            }

            foreach (IDirectory subDirectory in this.Directories)
            {
                IDirectory subTargetDirectory = targetDirectory.AddDirectory(subDirectory.Name);
                subDirectory.Copy(subTargetDirectory);
            }

            foreach (IFile file in this.Files)
            {
                IFile targetFile = targetDirectory.AddFile(file.Name);
                file.Copy(targetFile);
            }
        }

        public virtual void Move(IFileSystemObject target)
        {
            this.Copy(target);
            this.Delete();
        }

        public virtual void Delete()
        {
            this.internalObject.Delete();
        }

        public virtual void Rename(string newName)
        {
            this.internalObject.Rename(newName);
        }

        #endregion

        #region IFileSystemObject Members


        public int Size
        {
            get
            {
                int totalSize = 0;

                foreach (IDirectory subDirectory in this.Directories)
                {
                    totalSize += subDirectory.Size;
                }

                foreach (IFile file in this.Files)
                {
                    totalSize += file.Size;
                }

                return totalSize;
            }
        }

        #endregion
    }
}
